\#define MyAppName "${info.name}"
\#define MyAppVersion "${info.version}"
\#define MyAppPublisher "${info.organizationName}"
\#define MyAppURL "$!{info.organizationUrl}"
\#define MyAppExeName "${info.executable.name}"
\#define MyAppFolder "${info.name}"
\#define MyAppLicense "$!{info.licenseFile.absolutePath}"
\#define MyAppIcon "${info.iconFile.absolutePath}"
\#define MyShortcutName = "${info.winConfig.shortcutName}"

[Setup]
AppId={{{#MyAppName}}}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppURL}
AppUpdatesURL={#MyAppURL}
DefaultDirName={autopf}\{#MyAppFolder}
#if ($info.winConfig.disableDirPage)
DisableDirPage=yes
#else
DisableDirPage=no
#end
#if ($info.winConfig.disableProgramGroupPage)
DisableProgramGroupPage=yes
#else
DefaultGroupName={#MyAppName}
DisableProgramGroupPage=no
#end
#if ($info.winConfig.disableFinishedPage)
DisableFinishedPage=yes
#else
DisableFinishedPage=no
#end
#if ($info.winConfig.disableWelcomePage)
DisableWelcomePage=yes
#else
DisableWelcomePage=no
#end
#if ($info.winConfig.setupMode.name() == "installForAllUsers") 
PrivilegesRequired=admin
PrivilegesRequiredOverridesAllowed=commandline
#elseif ($info.winConfig.setupMode.name() == "installForCurrentUser") 
PrivilegesRequired=lowest
PrivilegesRequiredOverridesAllowed=commandline
#else 
PrivilegesRequiredOverridesAllowed=commandline dialog
#end
LicenseFile={#MyAppLicense}
SetupIconFile={#MyAppIcon}
UninstallDisplayIcon={app}\{#MyAppExeName}
Compression=lzma
SolidCompression=yes
ArchitecturesInstallIn64BitMode=x64

[Languages]
#foreach ($language in $info.winConfig.setupLanguages.entrySet())
Name: "${language.key}"; MessagesFile: "${language.value}"
#end

[Tasks]
#if ($info.winConfig.createDesktopIconTask)
Name: "desktopicon"; Description: "{cm:CreateDesktopIcon}"; GroupDescription: "{cm:AdditionalIcons}"; Flags: unchecked
#end

[Registry]
#if ($info.winConfig.registry)
#foreach ($entry in $info.winConfig.registry.entries)
Root: ${entry.root}; Subkey: "${entry.subkey}"; ValueType: ${entry.valueTypeAsInnoSetupString}; ValueName: "$!{entry.valueName}"; ValueData: "$!{entry.valueData}"; Flags: uninsdeletevalue
#end
#end
#foreach ($fileAssociation in $info.fileAssociations)
; ${fileAssociation.extension} extension file association
Root: HKA; Subkey: "Software\Classes\.${fileAssociation.extension}\OpenWithProgids"; ValueType: string; ValueName: "${info.name}.${fileAssociation.extension}"; ValueData: ""; Flags: uninsdeletevalue
Root: HKA; Subkey: "Software\Classes\\${info.name}.${fileAssociation.extension}"; ValueType: string; ValueName: ""; ValueData: "${fileAssociation.description}"; Flags: uninsdeletekey
Root: HKA; Subkey: "Software\Classes\\${info.name}.${fileAssociation.extension}\DefaultIcon"; ValueType: string; ValueName: ""; ValueData: "{app}\\${info.executable.name},0"; Flags: uninsdeletevalue
Root: HKA; Subkey: "Software\Classes\\${info.name}.${fileAssociation.extension}\shell\open\command"; ValueType: string; ValueName: ""; ValueData: """{app}\\${info.executable.name}"" ""%1"""; Flags: uninsdeletevalue
Root: HKA; Subkey: "Software\Classes\Applications\\${info.executable.name}\SupportedTypes"; ValueType: string; ValueName: ".${fileAssociation.extension}"; ValueData: ""; Flags: uninsdeletevalue
#end

[Files]
Source: "${info.appFolder}\*"; DestDir: "{app}"; Flags: ignoreversion recursesubdirs createallsubdirs

[Icons]
Name: "{autoprograms}\{#MyAppName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\\${info.executable.name}"
#if ($info.winConfig.createDesktopIconTask)
Name: "{autodesktop}\{#MyShortcutName}"; Filename: "{app}\{#MyAppExeName}"; IconFilename: "{app}\\${info.executable.name}"; Tasks: desktopicon
#end

[Run]
#if (!$info.winConfig.disableRunAfterInstall)
Filename: "{app}\{#MyAppExeName}"; Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; Flags: nowait postinstall skipifsilent runascurrentuser
#end

[Code]

function GetInstallLocation(): String;
var
    unInstPath: String;
    installLocation: String;
begin
    unInstPath := ExpandConstant('Software\Microsoft\Windows\CurrentVersion\Uninstall\{#emit SetupSetting("AppId")}_is1');
    installLocation := '';
    if not RegQueryStringValue(HKLM, unInstPath, 'InstallLocation', installLocation) then
        RegQueryStringValue(HKCU, unInstPath, 'InstallLocation', installLocation);
    Result := RemoveQuotes(installLocation);
end;

#if ($info.winConfig.removeOldLibs)
procedure RemoveOldLibs();
var
    installLocation: String;
    libsLocation: String;
begin
    installLocation := GetInstallLocation();
    if installLocation <> '' then 
    begin
        libsLocation := installLocation + '${info.libsFolder.name}';
        DelTree(libsLocation, True, True, True);
    end;
end;
#end

procedure CurStepChanged(CurStep: TSetupStep);
begin
    if CurStep = ssInstall then
    begin
#if ($info.winConfig.removeOldLibs)
        RemoveOldLibs();
#end
    end;
end;
